#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
import typing

from pyoi.context.abc import MQChannelABC
from pyoi.context.types import FederatedPacketRouting, FederatedPacketHeaders
from pyoi.util import log_utils

LOGGER = log_utils.getLogger()


class BaseMQChannel(MQChannelABC):
    def __init__(self, routing_info: FederatedPacketRouting, params: dict):
        self._routing_info = routing_info
        self._params = params
        self._num_partitions = params["num_partitions"]
        self._max_message_size = params["max_message_size"]
        self._role = None
        self._node_id = None
        self._send_topic = routing_info.send_topic
        self._receive_topic = routing_info.receive_topic
        self._group = routing_info.task_id
        self._conn = None
        self._channel = None

    @property
    def role(self) -> str:
        return self._role

    @property
    def node_id(self) -> str:
        return self._node_id

    @property
    def routing_info(self) -> FederatedPacketRouting:
        return self._routing_info

    @property
    def group(self) -> str:
        return self._group

    @group.setter
    def group(self, group: str):
        self._group = group

    @property
    def send_topic(self) -> str:
        return self._send_topic

    @property
    def receive_topic(self) -> str:
        return self._receive_topic

    def publish(self, headers: FederatedPacketHeaders, data: bytes):
        LOGGER.debug(f"publish to topic {self._send_topic}")
        return self.publish_to(topic=self._send_topic, headers=headers, data=data)

    def publish_to(self, topic: str, headers: FederatedPacketHeaders, data: bytes):
        raise NotImplementedError

    def consume(self) -> typing.Generator[object, FederatedPacketHeaders, bytes]:
        return self.consume_on(self._receive_topic)

    def consume_on(self, topic) -> typing.Generator[object, FederatedPacketHeaders, bytes]:
        raise NotImplementedError

    def ack(self, deliver: typing.Any, **kwargs):
        raise NotImplementedError

    def cancel(self):
        raise NotImplementedError

    def close(self):
        raise NotImplementedError
